package com.atjinggu.jinggumail.search.thread;

import java.util.concurrent.*;

/**
 * @author jingming
 * @Type ThreadTest.java
 * @Desc
 * @date 2021/2/16 22:58
 */
public class ThreadTest {
    public static ExecutorService service = Executors.newFixedThreadPool(10);



    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("主线程开始执行********");
        new ThreadPoolExecutor(5,
                200,
                10,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(10000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
                );

        /**
         * 1.Thread 方式
         */
//        Thread01 thread01 = new Thread01();
//        thread01.start();

        /**
         * 2.Runnable 方式
         */
//        new Thread(new Runnable01()).start();



        /**
         * 3.Callable方式
         */

//        Callable01 callable01 = new Callable01();
//        FutureTask<Integer> task = new FutureTask(callable01);
//        new Thread(task).start();
//        Integer integer = task.get();
//        System.out.println(integer);
        /**
         * 实际上这个result 是由你设置好传进去的,FutureTask 只是在Runnable 执行完之后返回预先设置好的result,以便通知任务已完成.
         * 可以查看源码:
         * static final class RunnableAdapter<T> implements Callable<T> {
         *     final Runnable task;
         *     final T result;
         *     RunnableAdapter(Runnable  task, T result) {
         *         this.task = task;
         *         this.result = result;
         *     }
         *     public T call() {
         *         task.run();
         *         return result;
         *     }
         * }
         */
//        Integer i = 2;
//        FutureTask<Integer> task = new FutureTask(new Runnable01(),i);
//        new Thread(task).start();
//        System.out.println(task.get());


        //Future<?> submit = service.submit(new Runnable01());

        //service.execute(new Runnable01());

        Callable01 callable01 = new Callable01();
//        Future<Integer> submit = service.submit(callable01);
//        Integer integer = submit.get();
//        System.out.println(integer);


        FutureTask<Integer> task = new FutureTask(callable01);
        Future<?> submit1 = service.submit(task);
        System.out.println(submit1.get());
        System.out.println(task.get());


        System.out.println("主线程执行结束********");
    }

    public static  class Thread01 extends Thread{

        @Override
        public void run() {
            System.out.println("当前线程Id ********* "+Thread.currentThread().getId());
           int i = 10 / 2;
            System.out.println(i);
        }
    }

    public static  class Runnable01 implements Runnable{

        @Override
        public void run() {
            System.out.println("当前线程Id ********* "+Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println(i);
        }
    }

    public static class Callable01 implements Callable<Integer>{

        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程Id ********* "+Thread.currentThread().getId());
            int i = 10 / 2;
            return i;
        }
    }
}